package cn.iocoder.yudao.module.shop.controller.admin.shopTable;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.shop.controller.admin.shopTable.vo.ShopTablePageReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.shopTable.vo.ShopTableRespVO;
import cn.iocoder.yudao.module.shop.controller.admin.shopTable.vo.ShopTableSaveReqVO;
import cn.iocoder.yudao.module.shop.dal.dataobject.shopTable.ShopTableDO;
import cn.iocoder.yudao.module.shop.service.shopTable.ShopTableService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 商户桌号")
@RestController
@RequestMapping("/dc/shop-table")
@Validated
public class ShopTableController {

    @Resource
    private ShopTableService tableService;

    @PostMapping("/create")
    @Operation(summary = "创建商户桌号")
    @PreAuthorize("@ss.hasPermission('shop:table:create')")
    public CommonResult<Long> createTable(@Valid @RequestBody ShopTableSaveReqVO createReqVO) {
        return success(tableService.createTable(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商户桌号")
    @PreAuthorize("@ss.hasPermission('shop:table:update')")
    public CommonResult<Boolean> updateTable(@Valid @RequestBody ShopTableSaveReqVO updateReqVO) {
        tableService.updateTable(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateBatch")
    @Operation(summary = "更新商户桌号")
    @PreAuthorize("@ss.hasPermission('dc:shop-open-time:update')")
    public CommonResult<Boolean> updateTableBatch(@Valid @RequestBody ShopTableSaveReqVO.ShopTableUpdateReqVO vo) {
        tableService.updateTableBatch(vo);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商户桌号")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:table:delete')")
    public CommonResult<Boolean> deleteTable(@RequestParam("id") Long id) {
        tableService.deleteTable(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商户桌号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:table:query')")
    public CommonResult<ShopTableRespVO> getTable(@RequestParam("id") Long id) {
        ShopTableDO table = tableService.getTable(id);
        return success(BeanUtils.toBean(table, ShopTableRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商户桌号分页")
    @PreAuthorize("@ss.hasPermission('shop:table:query')")
    public CommonResult<PageResult<ShopTableRespVO>> getTablePage(@Valid ShopTablePageReqVO pageReqVO) {
        PageResult<ShopTableDO> pageResult = tableService.getTablePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ShopTableRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得商户桌号列表")
    @PreAuthorize("@ss.hasPermission('dc:shop-open-time:query')")
    public CommonResult<List<ShopTableRespVO>> getTableList(@Valid ShopTablePageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShopTableDO> list = tableService.getTablePage(pageReqVO).getList();
        return success(BeanUtils.toBean(list, ShopTableRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商户桌号 Excel")
    @PreAuthorize("@ss.hasPermission('shop:table:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTableExcel(@Valid ShopTablePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShopTableDO> list = tableService.getTablePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商户桌号.xls", "数据", ShopTableRespVO.class,
                        BeanUtils.toBean(list, ShopTableRespVO.class));
    }

}